package com.lgl.scsapi.util.utils;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 日期工具类
 */
public class DateUtil {
	private static String dateRex = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-9]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";

	/**
	 * 获取当前日期
	 * @param yearFormat 年月日期格式,/,-，或空
	 * @return
	 */
	public static String getNowDate(String yearFormat) {
		String localDate = LocalDate.now().toString();
		if (!"-".equals(yearFormat)){
			return localDate.replaceAll("-",yearFormat);
		}
		return localDate;
	}

	/**
	 * 获取当前年月
	 * @param format 年月格式，如：-,/等
	 * @return 返回format格式的年月，如：2020-12,2020/12
	 */
	public static String getNowYearMonth(String format) {
		String localDate = LocalDate.now().toString();
		localDate = localDate.substring(0,7);
		format = format == null ? "" : format;
		if (!"-".equals(format)){
			return localDate.replaceAll("-",format);
		}
		return localDate;
	}
	/**
	 * 获取当前年月日，时分秒
	 * @param yearFormat 年月日期格式,/,-，或空
	 * @return
	 */
	public static String getNowDateTime(String yearFormat) {
		String localDateTime = LocalDateTime.now().toString();
		localDateTime = localDateTime.replace("T"," ").substring(0,localDateTime.lastIndexOf("."));
		if (!"-".equals(yearFormat)){
			return localDateTime.replaceAll("-",yearFormat);
		}
		return localDateTime;
	}

	/**
	* @Title: compareDate
	* @Description: TODO(日期比较，如果s>=e 返回true 否则返回false)
	* @param s
	* @param e
	* @return boolean  
	 */
	/*public staticFile boolean compareDate(String s, String e) {
		if (StringUtils.isEmpty(s) || StringUtils.isEmpty(e)) {
			return false;
		}
    	try {
			Date startDate = DateUtils.parseDate(s, dateParsePatterns);
			Date endDate = DateUtils.parseDate(e, dateParsePatterns);
			return startDate.getTime() >= endDate.getTime();
		} catch (ParseException e1) {
			return false;
		}
	}*/

	/**
	 * 校验日期字符串是否合法
	 * @param dateStr 日期字符串，格式：yyyy-mm-dd HH:mm:ss
	 * @return true:合法，false:不合法
	 */
	public static boolean isValidDate(String dateStr) {
		Pattern pattern = Pattern.compile(dateRex);
		Matcher matcher = pattern.matcher(dateStr);
		return matcher.matches();
	}
	/*public static int getDiffYear(String startTime,String endTime) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			int years=(int) (((fmt.parse(endTime).getTime()-fmt.parse(startTime).getTime())/ (1000 * 60 * 60 * 24))/365);
			return years;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return 0;
		}
	}*/
	  /**
     * <li>功能描述：时间相减得到天数
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long 
     * @author Administrator
     */
    /*public staticFile long getDaySub(String beginDateStr,String endDateStr){
        long day=0;
        java.util.Date beginDate = null;
        java.util.Date endDate = null;
        
        try {
			beginDate = DateUtils.parseDate(beginDateStr, dateParsePatterns);
			endDate= DateUtils.parseDate(endDateStr, dateParsePatterns);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
        return day;
    }*/
    
    /**
     * 得到n天之后的日期
     * @param days
     * @return
     */
    public static String getAfterDayDate(String days) {
    	int daysInt = Integer.parseInt(days);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = calendar.getTime();
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        return dateStr;
    }
    
    /**
     * 得到n天之后是周几
     * @param days
     * @return
     */
    public static String getAfterDayWeek(String days) {
    	int daysInt = Integer.parseInt(days);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);
        return dateStr;
    }

	/**
	 * 获取某年某月最后一天
	 * @param dateStr  日期字符串,格式：YYYY-MM-DD
	 * @param date  日期对象，当日期对象和日期字符串都不为空时，以字符串为准
	 * @return
	 */
    public static int getLastDayOfMonth(String dateStr, Date date){
    	Calendar calendar = Calendar.getInstance();
    	if (date != null){
    		calendar.setTime(date);
		}
    	if (StringUtils.hasText(dateStr)){
    		String year = dateStr.substring(0,4), month = dateStr.substring(5,7).replace("0","");
    		calendar.set(Calendar.YEAR,Integer.parseInt(year));
    		calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		}
    	return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

}